home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C / Applications / Painted Black 0.91 / src / HDSmain.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-12-27  |  6.9 KB  |  286 lines  |  [TEXT/CWIE]

  1. /******************************************************************************
  2.  
  3. Hub's Drop Shell 1.0
  4. ©Copyright 1995, Hubert Figuière, All right reserved.
  5.  
  6. January 29th, 1995
  7. Modified April 27th, 1995
  8.  
  9. Author :
  10.     Hubert Figuière                HF
  11.  
  12. Purpose : this a shell used for creating simple Drag'n Drop application
  13. Modified for Painted Black with generic and specific enhancements.
  14.  
  15. Features :
  16.     -relies on System 7
  17.     -implements menus and about Box
  18.     -support 4 required apple events
  19.     
  20. 01/21/95
  21.     -initial release (HF)
  22. 01/29/95
  23.     -moved AppleEvents stuff to HDSAEstuff.c (HF)
  24. 03/11/95
  25.     -compiled for CW 5.5 (HF)
  26. 03/18/95
  27.     -Modified specially for Painted Black (HF)
  28. 03/21/95
  29.     -added support for auto foreground switching (HF)
  30. 04/26/95
  31.     -finished support for auto PaintBlack... (HF)
  32. 04/27/95
  33.     -corrected bug with keymap (HF)
  34.  
  35. ******************************************************************************/
  36.  
  37.  
  38. #include <AppleEvents.h>
  39. #include <GestaltEqu.h>
  40.  
  41. #include "QuickQD.h"                        //for QuickQD Macros...
  42.  
  43. #include "HDSdefinitions.h"
  44. #include "HDSAEstuff.h"
  45. #include "HDSCustom.h"
  46.  
  47. #ifdef __powerc
  48.     QDGlobals qd;
  49. #endif
  50.  
  51. Boolean        finished = False;                //nothing else to do
  52. MenuHandle    menus [MENUNUM];                //menus in menubar
  53. short        gDrag_n_drop;                    //drag n drop launch
  54. RgnHandle    gMouse_moved_rgn;                //region for mouse-moved events
  55. long        gFadeDelay = 3600 * 5;            //5 minutes before fade
  56.  
  57.  
  58. //for functions prototypes, see HDSdefinitions.h
  59.  
  60.  
  61. void init()
  62. /*Initialize the application*/
  63. {
  64.     LongInt        info;                            /*gestalt response*/
  65.     Str255        msg;                            /*message displayed*/
  66.     Integer        i;                                //counter
  67.     RgnHandle    temp_rgn;                        /*temporary Rgn*/
  68.     Rect        temp_rect;                        /*temporary rect*/
  69.     
  70.     InitGraf(&qd.thePort);
  71.     InitFonts();
  72.     InitWindows();
  73.     InitMenus();
  74.     TEInit();
  75.     InitDialogs(NULL);
  76.     InitCursor();
  77.     MaxApplZone();
  78.     
  79.     FlushEvents(everyEvent,0);                /*flush all the OSEvents pending. High-Level Events are not affected*/
  80.     
  81.     if (Gestalt(gestaltAppleEventsAttr,&info) || !BitTst(&info, 31-gestaltAppleEventsPresent))
  82.     {                                        /* if no AppleEvent manager */
  83.         GetIndString(msg, kStringsID, kGestaltErrNum);
  84.         ParamText(msg, "\p", "\p", "\p");
  85. /*item*/StopAlert(kErrorALRTID, NULL);
  86.         ExitToShell();                        /* Exit app */
  87.     }
  88.     if (init_AE())
  89.     {
  90.         for (i=0;i<3;i++)
  91.             SysBeep(10);
  92.         ExitToShell();                        //quit after problems
  93.     }
  94.     for (i=0;i<MENUNUM;i++)
  95.     {
  96.         menus[i]=GetMenu(MAPPLEID+i);        //get the menu
  97.         InsertMenu(menus[i],0);                //insert the menu
  98.     }
  99.     AddResMenu(menus[0],'DRVR');            //add the DAs into the menu
  100.     DrawMenuBar();                            /*draw the menu bar */
  101.     
  102.     GetCurrentProcess(&gMy_pid);                //retrieve our process number
  103.     
  104.     //mouse-moved rgn
  105.     gMouse_moved_rgn=NewRgn();
  106.     temp_rgn=NewRgn();
  107.     SetRect(&temp_rect,qd.screenBits.bounds.left,qd.screenBits.bounds.bottom-1,qd.screenBits.bounds.left+1,qd.screenBits.bounds.bottom);
  108. //    InsetRect(&temp_rect,-2,-2);
  109.     RectRgn(temp_rgn,&temp_rect);
  110.     
  111.     RectRgn(gMouse_moved_rgn,&qd.screenBits.bounds);
  112.     /*
  113.     Here is how gMouse_moved_rgn looks like :
  114.     +--------------------------+
  115.     |                          |
  116.     |                          |
  117.     |                          |
  118.     |      Screen              |
  119.     |                          |
  120.     |                          |
  121.     |                          |
  122.     +                          |
  123. hot->\                         |
  124. spot  +------------------------+    
  125.     The point in the corner is the hot spot...
  126.     */
  127.     DiffRgn(gMouse_moved_rgn,temp_rgn,gMouse_moved_rgn);
  128. }
  129.  
  130.  
  131.  
  132. void display_error (short err_type, OSErr the_err)
  133. {
  134.     Str255    msg, err_str;
  135.     
  136.     GetIndString(msg, kStringsID, err_type);
  137.     NumToString(the_err, err_str);
  138.     ParamText(msg, err_str, "\p", "\p");
  139. /*item=*/
  140.     StopAlert(kErrorALRTID, NULL);
  141. }
  142.  
  143.  
  144. void do_about()
  145. {
  146.     Integer        item;                                /*item selected*/
  147.     DialogPtr    about_dlog;                            /*dialog that is going to be displayed*/
  148.  
  149.     about_dlog = GetNewDialog(kAboutDLOGID, NULL, (DialogPtr)-1);
  150.     ModalDialog(NULL, &item);
  151.     DisposeDialog(about_dlog);
  152. }
  153.  
  154.  
  155. void handle_os_event(EventRecord* evt)
  156. {
  157.     //0xFA000000
  158.     if (evt->message & 0xFA000000)    /* mouse moved event */
  159.             PaintedBlack();
  160. }
  161.  
  162.  
  163.  
  164. void handle_key(EventRecord* evt)
  165. {
  166.     if (evt->modifiers & cmdKey)
  167.         handle_menus(MenuKey(LoWord(evt->message) & charCodeMask));
  168. }
  169.  
  170.  
  171. void handle_menus(long selection)                    //handle menus
  172. {
  173.     long    final_ticks;                    /* number of ticks after Delay()*/
  174.     switch (HiWord(selection))
  175.     {
  176.         case MAPPLEID:
  177.             do_about();
  178.             break;
  179.         case MFILEID:
  180.             switch (LoWord(selection))
  181.             {
  182.                 case MFileOpen:
  183.                     Delay(FADE_NOW_DELAY,&final_ticks);    /* wait 1/4 of second */
  184.                     do_it();
  185.                     break;
  186.                 case MFileQuit:
  187.                     finished=True;
  188.             }/*switch*/
  189.     }/*switch*/
  190.     HiliteMenu(0);
  191. }
  192.  
  193.  
  194. void handle_mousedown(EventRecord* evt)
  195. {
  196.     WindowPtr    pointed_win;
  197.     
  198.     switch (FindWindow(evt->where,&pointed_win))
  199.     {
  200.         case inMenuBar:
  201.             handle_menus(MenuSelect(evt->where));
  202.             break;
  203.     }
  204. }
  205.  
  206.  
  207. void main()
  208. {
  209.     EventRecord            evt;                        /*event record of the received event*/
  210.     Point                mouse_pt;                    /*mouse loc*/
  211.     Point                previous_mouse_pt;
  212.     unsigned long        last_time;                    //last time an event occured;
  213.     unsigned long        current_time;                //current time
  214.     KeyMap                the_keys;                    //pressed keys
  215.  
  216.     init();                                            /*initialize the app*/
  217.  
  218.     GetMouse(&previous_mouse_pt);
  219.     LocalToGlobal(&previous_mouse_pt);
  220.     last_time = TickCount();
  221.     
  222.     while (!finished)
  223.     {
  224.         if(WaitNextEvent(EVTMASK, &evt, IDLETIME, gMouse_moved_rgn))    //wait for events
  225.         {
  226.             switch (evt.what)
  227.              {
  228.                  case kHighLevelEvent:                        /*there are AppleEvents*/
  229.         /*err =*/    AEProcessAppleEvent(&evt);    /*process the apple event*/
  230.                     break;
  231.                 case mouseDown:
  232.                     handle_mousedown(&evt);
  233.                     break;
  234.                 case keyDown:
  235.                     handle_key(&evt);
  236.                     break;
  237.                 case osEvt:
  238.                     handle_os_event(&evt);
  239.             }/*switch*/
  240.             //*(long *)&previous_mouse_pt = *(long *)&mouse_pt;        //SetPt() (faster)
  241.             QuickCopyPt(previous_mouse_pt, mouse_pt);
  242.             last_time = evt.when;                //an event occurred, so tell him
  243.         }
  244.         else
  245.         {
  246.             GetMouse(&mouse_pt);
  247.             LocalToGlobal(&mouse_pt);
  248.             current_time = TickCount();
  249.             // ATTENTION Partie Crade de C.... Faite pour comparer très vite et efficace...
  250.             //if (!(*(long *)&previous_mouse_pt == *(long *)&mouse_pt))    //faster than EqualPt
  251.             if (!QuickEqualPt(previous_mouse_pt, mouse_pt))
  252.             {                    // if mouse moved since last time...
  253.                 //*(long *)&previous_mouse_pt = *(long *)&mouse_pt;        //SetPt() (faster)
  254.                 QuickCopyPt(previous_mouse_pt, mouse_pt);
  255.                 last_time = current_time;
  256.                 if (switch_not_required && !PtInRgn(mouse_pt,gMouse_moved_rgn))
  257.                 /* only enter in this part if a switch is not yet required */
  258.                     obscure();
  259.             }
  260.             else
  261.             {
  262.                 GetKeys(the_keys);
  263.                 /*
  264.                 
  265.                 */
  266.                 if ( the_keys[0] || the_keys[1] || the_keys[2]
  267.                         || the_keys[3] || Button())                //check for key down or button
  268.                 {
  269.                     //(*(long *)&previous_mouse_pt = *(long *)&mouse_pt);        //SetPt() (faster)
  270.                     QuickCopyPt(previous_mouse_pt, mouse_pt);
  271.                     last_time = current_time;                    //event occured
  272.                 }
  273.                 else if (current_time - last_time >= gFadeDelay)
  274.                 {
  275.                     obscure();
  276.                     PaintedBlack();
  277.                     last_time = current_time;
  278.                 }
  279.             }
  280.         }
  281.  
  282.         if (gDrag_n_drop>=0)
  283.             gDrag_n_drop--;
  284.     }
  285. }
  286.